//
//  MASExampleTotalUpdateAnimateView.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/11.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "MASExampleTotalUpdateAnimateView.h"

@interface MASExampleTotalUpdateAnimateView()
@property (strong, nonatomic) UIView *superView;
@property (strong, nonatomic) UIView *sonView;
@property (assign, nonatomic) BOOL isExpanded;
@end

@implementation MASExampleTotalUpdateAnimateView

- (id)init {
    self = [super init];
    if (!self) return nil;
    
    self.superView = [LayoutUtils createView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
    [self.superView addGestureRecognizer:tap];
    [self addSubview:self.superView];
    
    self.sonView = [LayoutUtils createView];
    [self addSubview:self.sonView];
    
    [self setNeedsUpdateConstraints];
    
    return self;
}

-(void)updateConstraints{
    [self.superView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(kPadding);
        make.right.equalTo(kPadding);
        if (self.isExpanded) {
            make.bottom.equalTo(kPadding);
        } else {
            make.bottom.mas_equalTo(-300);
        }
    }];
    
    [self.sonView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.superView);
        // 这里使用优先级处理
        // 设置其最大值为250，最小值为90
        if (!self.isExpanded) {
            make.width.height.mas_equalTo(100 * 0.5).priorityLow();
        } else {
            make.width.height.mas_equalTo(100 * 3).priorityLow();
        }
        make.width.height.lessThanOrEqualTo(@250);// 最大值为250
        make.width.height.greaterThanOrEqualTo(@90);// 最小值为90
    }];
    
    [super updateConstraints];
}

-(void)onTap{
    self.isExpanded = !self.isExpanded;
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}
@end
